﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace FourPartHarmony3 {
    /// <summary>
    /// MidiSettingsWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MidiSettingsWindow : Window {
        MidiOutput mMidiOutput = new MidiOutput();

        public int SelectedDeviceId { get; set; }

        public int Velocity { get; set; }

        public MidiSettingsWindow(int selectedDeviceId, int velocity) {
            InitializeComponent();
            SelectedDeviceId = selectedDeviceId;
            Velocity = velocity;
        }

        private void ButtonOK_Click(object sender, RoutedEventArgs e) {
            int velocity;
            if (!Int32.TryParse(mTextBoxVelocity.Text, out velocity) || velocity < 0 || 127 < velocity) {
                MessageBox.Show("Error: velocity must be the integer number from 0 to 127.");
                mTextBoxVelocity.Text = "64";
                return;
            }

            Velocity = velocity;
            SelectedDeviceId = mListBoxOutputDevices.SelectedIndex;

            this.DialogResult = true;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e) {
            mTextBoxVelocity.Text = string.Format("{0}", Velocity);

            var list = mMidiOutput.GetDeviceList();

            mListBoxOutputDevices.Items.Clear();
            foreach (var item in list) {
                mListBoxOutputDevices.Items.Add(item.Item2);
            }
        }
    }
}
